package com.service.scheduling.support;

import com.service.dao.model.MemberStore;
import com.service.scheduling.constants.RedisLockConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年11月26日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Component
public class MemberStoreLockUtils extends RedissonLockUtils {

    public boolean lock(@NotNull Integer memberId, @NotNull Integer storeId) {
        return super.lock(buildStoreKey(memberId, storeId));
    }

    public void unlock(@NotNull Integer memberId, @NotNull Integer storeId) {
        super.unlock(buildStoreKey(memberId, storeId));
    }

    public static String buildStoreKey(Integer memberId, Integer storeId) {
        return RedisLockConstant.STORE_PRE + memberId + RedisLockConstant.INTERVAL + storeId;
    }

    public static String[] buildStoreKey(MemberStore... memberStore) {
        return Arrays.stream(memberStore).map(item -> buildStoreKey(item.getMemberId(), item.getId()))
                .toArray(item -> new String[memberStore.length]);
    }

    public static String[] buildStoreKey(List<MemberStore> storeList) {
        return storeList.stream().map(item -> buildStoreKey(item.getMemberId(), item.getId()))
                .toArray(item -> new String[storeList.size()]);
    }

    public static List<String> buildStoreKey2List(List<MemberStore> storeList) {
        return storeList.stream().map(item -> buildStoreKey(item.getMemberId(), item.getId())).collect(Collectors.toList());
    }

}
